package org.zhuxian.cn.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.zhuxian.cn.entity.DTO.EquipmentBuildsDTO;

import java.io.IOException;
import java.util.List;

@Data
@TableName("equipment_builds")
@JsonIgnoreProperties(ignoreUnknown = true)
@Schema(description = "装备配装实体类，包含配装的基本信息和装备、镶嵌物列表")
public class EquipmentBuilds {

    @Schema(description = "配装ID，自动生成", example = "1")
    @TableId(type = IdType.AUTO)
    private Integer id;

    @Schema(description = "用户ID", example = "10001")
    @TableField("user_id")
    private Long userId;

    @Schema(description = "配装职业", example = "青云")
    @TableField("build_class")
    private String buildClass;

    @Schema(description = "配装流派", example = "输出流")
    @TableField("build_school")
    private String buildSchool;

    @Schema(description = "配装名称", example = "青云输出配装")
    @TableField("build_name")
    private String buildName;

    @Schema(description = "配装描述", example = "这是一套注重输出的青云配装")
    @TableField("description")
    private String description;

    @Schema(description = "装备JSON字符串", example = "[{\"id\": 1, \"name\": \"武器名称\"}]")
    @TableField("equipments")
    private String equipmentsStr;

    @Schema(description = "灌注符JSON字符串", example = "[{\"id\": 1, \"name\": \"镶嵌物名称\"}]")
    @TableField("inlays")
    private String inlaysStr;

    @Schema(description = "点赞数统计", example = "100")
    @TableField("like_count_statistics")
    private Integer likeCountStatistics;

    @Schema(description = "装备列表", example = "[{\"id\": 1, \"name\": \"武器名称\"}]")
    @TableField(exist = false)
    private List<Equipment> equipments;

    @Schema(description = "镶嵌物列表", example = "[{\"id\": 1, \"name\": \"镶嵌物名称\"}]")
    @TableField(exist = false)
    private List<Inlay> inlays;

    @Schema(description = "法宝id", example = "123")
    @TableField("a_magic_weapon_id")
    private Integer aMagicWeaponId;

    @Schema(description = "天书")
    @TableField("skill_tree")
    private String skillTreeStr;

    @Schema(description = "天书对象")
    @TableField(exist = false)
    private EquipmentBuildsDTO.SkillTree skillTree;

    @Schema(description = "法宝等级属性")
    private String talismanLevelAttribute;

    @Schema(description = "星魄")
    private String starSpirit;

    @Schema(description = "套装融合")
    private String setFusion;

    @Schema(description = "天辰属性")
    private String galaxyAttribute;

    @Schema(description = "随机属性")
    private String randomValue;


    /**
     * 设置装备JSON字符串，同时解析并设置装备列表
     * @param equipmentsStr 装备JSON字符串
     */
    public void setEquipmentsStr(String equipmentsStr) {
        this.equipmentsStr = equipmentsStr;
        try {
            this.equipments = new ObjectMapper().readValue(equipmentsStr, new TypeReference<List<Equipment>>() {});
        } catch (IOException e) {
            this.equipments = null;
        }
    }

    /**
     * 设置镶嵌物JSON字符串，同时解析并设置镶嵌物列表
     * @param inlaysStr 镶嵌物JSON字符串
     */
    public void setInlaysStr(String inlaysStr) {
        this.inlaysStr = inlaysStr;
        try {
            this.inlays = new ObjectMapper().readValue(inlaysStr, new TypeReference<List<Inlay>>() {});
        } catch (IOException e) {
            this.inlays = null;
        }
    }

    /**
     * 设置装备列表，同时序列化为JSON字符串
     * @param equipments 装备列表
     */
    public void setEquipments(List<Equipment> equipments) {
        this.equipments = equipments;
        try {
            this.equipmentsStr = new ObjectMapper().writeValueAsString(equipments);
        } catch (IOException e) {
            this.equipmentsStr = null;
        }
    }

    /**
     * 设置镶嵌物列表，同时序列化为JSON字符串
     * @param inlays 镶嵌物列表
     */
    public void setInlays(List<Inlay> inlays) {
        this.inlays = inlays;
        try {
            this.inlaysStr = new ObjectMapper().writeValueAsString(inlays);
        } catch (IOException e) {
            this.inlaysStr = null;
        }
    }

    /**
     * 设置天书JSON字符串，同时解析并设置天书对象
     * @param skillTreeStr 天书JSON字符串
     */
    public void setSkillTreeStr(String skillTreeStr) {
        this.skillTreeStr = skillTreeStr;
        try {
            this.skillTree = new ObjectMapper().readValue(skillTreeStr, EquipmentBuildsDTO.SkillTree.class);
        } catch (IOException e) {
            this.skillTree = null;
        }
    }

    /**
     * 设置天书对象，同时序列化为JSON字符串
     * @param skillTree 天书对象
     */
    public void setSkillTree(EquipmentBuildsDTO.SkillTree skillTree) {
        this.skillTree = skillTree;
        try {
            this.skillTreeStr = new ObjectMapper().writeValueAsString(skillTree);
        } catch (IOException e) {
            this.skillTreeStr = null;
        }
    }
}